package org.liufeng.course.util;

import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.liufeng.course.message.resp.Article;
import org.liufeng.course.message.resp.ImageMessage;
import org.liufeng.course.message.resp.MusicMessage;
import org.liufeng.course.message.resp.NewsMessage;
import org.liufeng.course.message.resp.TextMessage;
import org.liufeng.course.message.resp.VideoMessage;
import org.liufeng.course.message.resp.VoiceMessage;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

//import com.thoughtworks.xstream.XStream;
//import com.thoughtworks.xstream.core.util.QuickWriter;
//import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
//import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
//import com.thoughtworks.xstream.io.xml.XppDriver;

/**
 * ???????????
 * 
 * @author liufeng
 * @date 2013-09-15
 */
public class MessageUtil {
	// ???????????????
	public static final String REQ_MESSAGE_TYPE_TEXT = "text";
	// ??????????????
	public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
	// ????????????????
	public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
	// ???????????????
	public static final String REQ_MESSAGE_TYPE_VIDEO = "video";
	// ????????????????λ??
	public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
	// ????????????????
	public static final String REQ_MESSAGE_TYPE_LINK = "link";

	// ???????????????????
	public static final String REQ_MESSAGE_TYPE_EVENT = "event";

	// ????????subscribe(????)
	public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
	// ????????unsubscribe(???????)
	public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
	// ????????scan(???????????????????????)
	public static final String EVENT_TYPE_SCAN = "scan";
	// ????????LOCATION(???????λ??)
	public static final String EVENT_TYPE_LOCATION = "LOCATION";
	// ????????CLICK(???????)
	public static final String EVENT_TYPE_CLICK = "CLICK";

	// ??????????????
	public static final String RESP_MESSAGE_TYPE_TEXT = "text";
	// ?????????????
	public static final String RESP_MESSAGE_TYPE_IMAGE = "image";
	// ???????????????
	public static final String RESP_MESSAGE_TYPE_VOICE = "voice";
	// ??????????????
	public static final String RESP_MESSAGE_TYPE_VIDEO = "video";
	// ???????????????
	public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
	// ??????????????
	public static final String RESP_MESSAGE_TYPE_NEWS = "news";

	/**
	 * ????????????????XML??
	 * 
	 * @param request
	 * @return Map<String, String>
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
		// ??????????洢??HashMap??
		Map<String, String> map = new HashMap<String, String>();

		// ??request???????????
		InputStream inputStream = request.getInputStream();
		// ?????????
		SAXReader reader = new SAXReader();
		Document document = reader.read(inputStream);
		// ???xml?????
		Element root = document.getRootElement();
		// ?????????????????
		List<Element> elementList = root.elements();

		// ????????????
		for (Element e : elementList)
			map.put(e.getName(), e.getText());

		// ??????
		inputStream.close();
		inputStream = null;

		return map;
	}

	/**
	 * ???xstream??????CDATA
	 */
	private static XStream xstream = new XStream(new XppDriver() {
		public HierarchicalStreamWriter createWriter(Writer out) {
			return new PrettyPrintWriter(out) {
				// ??????xml?????????????CDATA???
				boolean cdata = true;

				@SuppressWarnings("unchecked")
				public void startNode(String name, Class clazz) {
					super.startNode(name, clazz);
				}

				protected void writeText(QuickWriter writer, String text) {
					if (cdata) {
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					} else {
						writer.write(text);
					}
				}
			};
		}
	});

	/**
	 * ???????????????xml
	 * 
	 * @param textMessage ??????????
	 * @return xml
	 */
	public static String messageToXml(TextMessage textMessage) {
		xstream.alias("xml", textMessage.getClass());
		return xstream.toXML(textMessage);
	}

	/**
	 * ??????????????xml
	 * 
	 * @param imageMessage ?????????
	 * @return xml
	 */
	public static String messageToXml(ImageMessage imageMessage) {
		xstream.alias("xml", imageMessage.getClass());
		return xstream.toXML(imageMessage);
	}

	/**
	 * ????????????????xml
	 * 
	 * @param voiceMessage ???????????
	 * @return xml
	 */
	public static String messageToXml(VoiceMessage voiceMessage) {
		xstream.alias("xml", voiceMessage.getClass());
		return xstream.toXML(voiceMessage);
	}

	/**
	 * ???????????????xml
	 * 
	 * @param videoMessage ??????????
	 * @return xml
	 */
	public static String messageToXml(VideoMessage videoMessage) {
		xstream.alias("xml", videoMessage.getClass());
		return xstream.toXML(videoMessage);
	}

	/**
	 * ????????????????xml
	 * 
	 * @param musicMessage ???????????
	 * @return xml
	 */
	public static String messageToXml(MusicMessage musicMessage) {
		xstream.alias("xml", musicMessage.getClass());
		return xstream.toXML(musicMessage);
	}

	/**
	 * ???????????????xml
	 * 
	 * @param newsMessage ??????????
	 * @return xml
	 */
	public static String messageToXml(NewsMessage newsMessage) {
		xstream.alias("xml", newsMessage.getClass());
		xstream.alias("item", new Article().getClass());
		return xstream.toXML(newsMessage);
	}
}
